package com.fenda.qq;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class QQClient {

	public static void register(String user, String pwd) {
		try {
			//1.建立到服务器9999端口的连接
			Socket socket = new Socket("127.0.0.1",9999);
			//2.获取JVM到socket的输出流
			OutputStream os = socket.getOutputStream();
			//3.将原生的字节输出流封装为对象输出流
			ObjectOutputStream oos = new ObjectOutputStream(os);
			//4.封装请求参数QQRequest
			QQRequest request = new QQRequest();
			request.setOperator(1);//1代表注册操作
			request.setUsername(user);
			request.setPassword(pwd);
			//5.将请求对象request写入socket
			oos.writeObject(request);
			oos.flush();
			//6.接收注册结果（服务器返回的）
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//封装原生输入流为对象流，方便对象读取
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			System.out.println(resp);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static QQResponse login(String user, String password) {

		try {
			//1.连接服务器
			Socket socket = new Socket("127.0.0.1",9999);
			//2.获取输出流并封装为对象流
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest(2,user,password);//2代表登录操作
			oos.writeObject(request);
			oos.flush();
			//3.接收注册结果（服务器返回的）
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//封装原生输入流为对象流，方便对象读取
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			System.out.println(resp);
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static QQResponse addFriend(String user, String friend) {
		//1.连接服务器
		try {
			//1.连接服务器
			Socket socket = new Socket("127.0.0.1",9999);
			//2.获取输出流并封装为对象流
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();//2代表登录操作
			request.setOperator(3);//添加好友
			request.setUsername(user);
			request.setFriend(friend);
			oos.writeObject(request);
			oos.flush();
			//6.接收注册结果（服务器返回的）
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//封装原生输入流为对象流，方便对象读取
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static QQResponse findFriendsInfo(String user) {
		//1.连接服务器
		try {
			//1.连接服务器
			Socket socket = new Socket("127.0.0.1",9999);
			//2.获取输出流并封装为对象流
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();
			request.setOperator(4);//查询好友列表
			request.setUsername(user);
			oos.writeObject(request);
			oos.flush();
			//6.接收注册结果（服务器返回的）
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//封装原生输入流为对象流，方便对象读取
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

    public static QQResponse sendMsg(String user, String friend, String show) {
		try {
			//1.连接服务器
			Socket socket = new Socket("127.0.0.1",9999);
			//2.获取输出流并封装为对象流
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();
			request.setOperator(5);//发送聊天消息
			request.setUsername(user);
			request.setFriend(friend);
			request.setMsg(show);
			oos.writeObject(request);
			oos.flush();
			//6.接收注册结果（服务器返回的）
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//封装原生输入流为对象流，方便对象读取
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
    }

	public static QQResponse findChatLog(String user, String friend) {
		try {
			//1.连接服务器
			Socket socket = new Socket("127.0.0.1",9999);
			//2.获取输出流并封装为对象流
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();
			request.setOperator(6);//查找聊天记录
			request.setUsername(user);
			request.setFriend(friend);
			oos.writeObject(request);
			oos.flush();
			//6.接收注册结果（服务器返回的）
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//封装原生输入流为对象流，方便对象读取
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
